So if you remember the image generator class that
we used earlier, it actually has the ability
to do this for you. Indeed, you have already done a little image augmentation with it when you're
re-scaled upon loading. That saved you from
converting all of your images on the file system
and then loading them in, you just re-scaled on the fly. So let's take a look at
some of the other options. Here's how you could
use a whole bunch of image augmentation options with the image generator
adding onto re-scale. Rotation range is a range from 0-180 degrees with which
to randomly rotate images. So in this case,
the image will rotate by random amount
between 0 and 40 degrees. Shifting, moves the image
around inside its frame. Many pictures have
the subject centered. So if we train based on
those kind of images, we might over-fit
for that scenario. These parameters specify, as a proportion of the image size, how much we should randomly
move the subject around. So in this case, we
might offset it by 20 percent vertically
or horizontally. Shearing is also quite powerful. So for example, consider
the image on the right. We know that it's a person. But in our training set, we don't have any images of
a person in that orientation. However, we do have
an image like this one, where the person is
oriented similarly. So if we shear that person
by skewing along the x-axis, we'll end up in a similar pose. That's what the shear_range
parameter gives us. It will shear the image by random amounts up to the
specified portion in the image. So in this case, it will shear up to 20 percent of the image. Zoom can also be very effective. For example, consider
the image on the right. It's obviously a woman
facing to the right. Our image on the left is from the humans or horses
training set. It's very similar but it zoomed out to see
the full person. If we zoom in on
the training image, we could end up with
a very similar image to the one on the right. Thus, if we zoom while training, we could spot more generalized
examples like this one. So you zoom with code like this. The 0.2 is a relative portion of the image you will zoom in on. So in this case, zooms will be a random amount up to 20 percent of
the size of the image. Another useful tool is
horizontal flipping. So for example, if you consider
the picture on the right, we might not be able to
classify it correctly as our training data
doesn't have the image of a woman with
her left hand raised, it does have the image
on the left, where the subjects
right arm is raised. So if the image were
flipped horizontally, then it becomes more structurally
similar to the image on the right and we might not
over-fit to right arm raisers. To turn on random
horizontal flipping, you just say
horizontal_flip equals true and the images will
be flipped at random. Finally, we just
specify the fill mode. This fills in any pixels that might have been lost
by the operations. I'm just going to stick
with nearest here, which uses neighbors of that pixel to try
and keep uniformity. Check the carets documentation
for some other options. So that's the concept
of image augmentation. Let's now take a look at
cats versus dogs that are trained with and
without augmentation, so that we can see
the impact that this has.